package com.example.martian.service;

import com.example.martian.huobi.client.MarketClient;
import com.example.martian.huobi.client.req.market.MarketTradeRequest;
import com.example.martian.huobi.model.account.Account;
import com.example.martian.huobi.model.market.MarketTrade;
import com.example.martian.huobi.service.HuobiAccountService;
import com.example.martian.huobi.service.HuobiGenericService;
import com.example.martian.mapper.IndexMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

@Service
public class HuobiService {

    @Autowired(required = false)
    private HuobiGenericService huobiGenericService;

    @Autowired
    private MarketClient marketClient;

    @Autowired
    private HuobiAccountService accountService;

    @Autowired
    private IndexMapper indexMapper;

    private static final String[] DEF_SYMBOLS = new String[]{"ethbtc","btcusdt","filbtc","ltcbtc","ethusdt","fileth",
            "filusdt","ltcusdt"};


    //获取所有币种
    @Transactional
    public Object getCurrencys(){
        return huobiGenericService.getCurrencys();
    }

    //获取所有币种对
    @Transactional
    public Object getSymbols(){
        return huobiGenericService.getSymbols();
    }
    public List<MarketTrade> getMarketTrade(){
        String symbols = "";
        String[] symbolArr;
        if(StringUtils.isBlank(symbols)){
            symbolArr = DEF_SYMBOLS;
        }else {
            symbolArr = symbols.split(",");
        }
        List<MarketTrade> trades = new ArrayList<>();

        Stream.of(symbolArr).forEach(symbol->{
            List<MarketTrade> marketTrade = marketClient.getMarketTrade(new MarketTradeRequest(symbol));
            MarketTrade trade = marketTrade.get(0);
            trade.setSymbol(symbol);
            trades.add(trade);

        });
        MarketTrade marketTrade = trades.get(1);
        System.out.println(marketTrade);
        return trades;
    }

    //更新汇率
    @Scheduled(cron = "0 */30 * * * *")
    @Transactional
    public void updateRate(){
        List<MarketTrade> marketTrade = getMarketTrade();
        System.out.println(marketTrade);
        List<Map<String, Object>> maps = indexMapper.showExchange();
        for (Map<String,Object> map : maps){
            String coins = map.get("coins").toString();
            for (int flg = 0;flg < marketTrade.size();flg ++){
                MarketTrade trade = marketTrade.get(flg);
                if (trade.getSymbol().equalsIgnoreCase(coins)){
                    String exangeRate = trade.getPrice().toString();
                    String coin = trade.getSymbol();
                    indexMapper.updageRate(exangeRate,coin);
                }
            }
        }
    }

    @Transactional
    public List<Account> getAccountId(){
        List<Account> accounts = accountService.getAccounts();
        return accounts;
    }

}
